package com.huoyun.tag;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ui.AbstractUITag;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.huoyun.base.bean.NodeInfo;
import com.huoyun.base.service.NodeInfoService;
import com.opensymphony.xwork2.util.ValueStack;

public class CustomSelectTag extends AbstractUITag {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2965750724100509863L;
	
	private NodeInfoService nodeInfoService;
	
	private String id;
	
    private String name;
	
	private String value;
	
	private boolean selectOne;
	
	/**
	 * 下拉选择框的类型
	 */
	private String mode;
	
	/**
	 * 类型：view表示直接展示省市区名称，没有下拉选择框，select表示下拉选择框显示，默认为下拉选择框
	 */
	private String type;

	@Override
	public Component getBean(ValueStack valueStack, HttpServletRequest request,
			HttpServletResponse response) 
	{
		return new CustomSelectBean(valueStack, request, response);
	}
	
	@Override  
    protected void populateParams() {  
        super.populateParams();  
        CustomSelectBean customSelectBean = (CustomSelectBean) component;
        customSelectBean.setId(id);
        customSelectBean.setName(name);
        customSelectBean.setSelectOne(selectOne);
        customSelectBean.setType(type);
        
        if("view".equals(type))
        {
        	 if(StringUtils.isNotEmpty(value))
             {
        		 customSelectBean.setValue(getNodeInfo(Integer.parseInt(value)).getName());
             }
        }
        else
        {
        	customSelectBean.setNodeList(getAllNodes());
        	
            if(StringUtils.isNotEmpty(value))
            {
            	customSelectBean.setValue(value);
            }
        } 
    }

	private List<NodeInfo> getAllNodes()
	{
		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		NodeInfoService nodeInfoService = (NodeInfoService)wac.getBean("nodeInfoService");
		
		return nodeInfoService.getTopNodesByMode(mode);
	}
	
	private NodeInfo getNodeInfo(int nodeId)
	{
		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		NodeInfoService nodeInfoService = (NodeInfoService)wac.getBean("nodeInfoService");
		
		return nodeInfoService.getNodeInfoById(nodeId);
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public NodeInfoService getNodeInfoService() {
		return nodeInfoService;
	}

	public void setNodeInfoService(NodeInfoService nodeInfoService) {
		this.nodeInfoService = nodeInfoService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public boolean isSelectOne() {
		return selectOne;
	}

	public void setSelectOne(boolean selectOne) {
		this.selectOne = selectOne;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	} 
	
	
}
